#include "stdafx.h"
#include "Distribution.h"


double Distribution::rand_e(double lambda)
{
	double x = ((double) rand()) / (double(RAND_MAX)+1.0);
	return -log(1.0-x) * lambda;
}

double Distribution::rand_n(double ev, double sigma)
{
	double x=  (double(RAND_MAX)+1.0);
	double ret;
	do {
		ret = sigma*sqrt(-2.0*log(((double)rand() + 1.0)/x))*sin(2.0*3.1415*rand()/x)+ev;
	} while(ret<=0);

	return ret;
}

double Distribution::generate()
{
	if (type == Distribution::NORMAL)
		return rand_n(a, b);
	else if (type == Distribution::EXPONENTIAL)
		return rand_e(a);
	else if (type == Distribution::DISCREET)
		return rand_d(a);
	return -1.0;
}